<?php
/**
 * Created by yaoyu.
 * User: Administrator
 * Date: 2020/4/2
 * Time: 16:46
 */

namespace App\Admin\Controllers;


use App\Model\Invest;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Routing\Controller;

class InvestController extends Controller
{
        use HasResourceActions;

        /**
         * @param Content $content
         * @return Content
         */
        public function index(Content $content)
        {
            return $content->header('企业列表')
                ->body($this->grid());
        }

        /**
         * @param Content $content
         * @return Content
         */
        public function create(Content $content)
        {
            return $content->header('create')->body($this->form());
        }

        /**
         * @param $id
         * @param Content $content
         * @return Content
         */
        public function edit($id, Content $content)
        {
            return $content
                ->header('编辑')
                ->description('描述')
                ->body($this->form()->edit($id));
        }

        /**
         * @return Form
         */
        public function form()
        {
            $form = new Form(new Invest());

            $form->text('title','标题');
            $form->file('logo', 'logo图');
            $form->file('imgurl', '图片');
            $form->text('summarize','简介');
            $form->textarea('content','内容');
            $form->display('id','新闻ID');

            return $form;
        }

        /**
         * 数据
         * @return Grid
         */
        public function grid()
        {
            $grid = new Grid(new Invest());

            $grid->id()->sortable();
            $grid->title('标题');
            $grid->summarize('简介');
            $grid->created_at('创建时间');
            $grid->updated_at('修改时间');
            return $grid;
        }

        /**
         * @param $id
         * @param Content $content
         * @return Content
         */
        public function show($id, Content $content)
        {
            return $content->header('详情')
                ->body($this->detail($id));
        }

        /**
         * @param $id
         * @return Show
         */
        public function detail($id)
        {
            $show = new Show(Invest::findOrFail($id));

            $show->id('ID');
            $show->title('标题');
            $show->summarize('简介');
            $show->content('内容');
            $show->logo('logo图')->setEscape(false)->as(function($logo){
                $path = env('APP_URL');
                $data='';
                return $data . "<img src='{$path}/upload/{$logo}' width='100'/>";
            });
            $show->imgurl('图片')->setEscape(false)->as(function($img_url){
                $path = env('APP_URL');
                $data='';
                return $data . "<img src='{$path}/upload/{$img_url}' width='300'/>";
            });

            $show->created_at('创建时间');
            $show->updated_at('修改时间');

            return $show;
        }
}